#!/usr/bin/env bash
#macOS获得本地IP
local_ip=`ifconfig en0 | grep "inet\ " | awk '{ print $2}'`

pushd `dirname $0` > /dev/null
BASE_DIR=`pwd -P`
popd > /dev/null
OPT_ENV_FORCE=$1

#############
# Functions
#############
function logging {
    echo "[INFO] $*"
}

function build_venv {    
    if [ ! -d env ]; then
        virtualenv env
    fi
    . env/bin/activate
    pip3 install -r requirements.txt  
}

cd ${BASE_DIR}
build_venv
cd ${BASE_DIR}/mysite

if [ "${OPT_ENV_FORCE}x" == "-ix" ];then 
    logging "./start.sh -i"
    rm -rf db.sqlite3
    rm -rf blog/migrations/0*.py
    python manage.py makemigrations
    python manage.py migrate
    python manage.py flush --noinput
    python initdb.py
    rm -rf static
    python manage.py collectstatic 
    logging "./start.sh -i end"   
fi

#python manage.py runserver $local_ip:8080
python thread_start.py
